package com.photographysharing.controller;


import com.photographysharing.dto.PhotoDTO;
import com.photographysharing.entity.Categories;
import com.photographysharing.exception.ServiceException;
import com.photographysharing.request.SelectByPaging;
import com.photographysharing.response.DatasResponse;
import com.photographysharing.response.MessageResponse;
import com.photographysharing.service.CategoriesService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {
    private CategoriesService categoriesService;

    public CategoryController(CategoriesService categoriesService) {
        this.categoriesService = categoriesService;
    }

    @RequestMapping("/add")
    public MessageResponse add(@RequestBody Categories category){
        try{
            if(categoriesService.save(category)){
                return new MessageResponse("新增成功!");
            }else{
                return new MessageResponse("新增失败!");
            }
        }catch (Exception e){
            throw new ServiceException(e.getMessage());
        }
    }

    @RequestMapping("/update")
    public MessageResponse update(@RequestBody Categories category){
        try{
            if(categoriesService.saveOrUpdate(category)){
                return new MessageResponse("更新成功!");
            }else{
                return new MessageResponse("更新失败!");
            }
        }catch (Exception e){
            throw new ServiceException(e.getMessage());
        }
    }


    @DeleteMapping("/delete")
    public MessageResponse update(Integer id){
        try{
            if(categoriesService.removeById(id)){
                return new MessageResponse("删除成功!");
            }else{
                return new MessageResponse("删除失败!");
            }
        }catch (Exception e){
            throw new ServiceException(e.getMessage());
        }
    }


    @GetMapping("/get")
    public DatasResponse<Categories> get(){
        List<Categories> datas = categoriesService.list();
        return new DatasResponse<>(datas.size(),datas);
    }

    @GetMapping("/selectByPag")
    public DatasResponse<Categories> selectByPag(SelectByPaging pag){
        return categoriesService.selectByPag(pag);
    }




}
